package com.ydgk.controller;

import com.ydgk.bean.Furniture;
import com.ydgk.bean.Order;
import com.ydgk.service.MyOrdersService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.AjaxResult;
import com.ydgk.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/myOrders")
@Controller
public class MyOrdersController {

    @Autowired
    private MyOrdersService myOrdersService;

    private static final Integer pageSize = 10;

    @ModelAttribute
    public void getOrder(@RequestParam(value = "oid",required = false,defaultValue = "0")Integer oid, Map map){
        if(oid != 0){
            Order order = myOrdersService.findOrderById(oid);
            map.put("order",order);
        }
    }

    @ResponseBody
    @RequestMapping("/findOrder")
    public AjaxResult findOrder(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "likeName", required = false, defaultValue = "") String likeName,
                                        @RequestParam(value = "otype", required = false, defaultValue = "") Integer otype,
                                        @RequestParam(value = "methodName", required = false, defaultValue = "")String methodName){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            Map<String, Object> factorMap = new HashMap<String, Object>();
            factorMap.put("otype", otype);
            factorMap.put("likeName",likeName);
            int count = myOrdersService.findAllOrderCount(factorMap);
            Integer pageTotal = 0;
            if(count <= pageSize){
                pageTotal = 1;
            }else{
                if (count % pageSize == 0) {
                    pageTotal = count / pageSize;
                } else {
                    pageTotal = count / pageSize + 1;
                }
            }
            if(pageNo > pageTotal){
                pageNo = pageTotal;
            }
            if(pageNo < 1){
                pageNo = 1;
            }

            Integer pageStart = (pageNo - 1) * pageSize;
            factorMap.put("start", pageStart);
            factorMap.put("size", pageSize);
            List<Order> orderList = myOrdersService.findAllOrder(factorMap);
            String pageContent = Tool.getPageContentForManager(pageNo,pageTotal,methodName);
            Page<Order> page = new Page(orderList, pageNo, pageTotal,count,pageContent);
            ajaxResult.setData(page);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deleteOrder")
    public AjaxResult deleteOrder(Integer oid){
        myOrdersService.deleteOrder(oid);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deleteOrders")
    public AjaxResult deleteOrders(@RequestParam(value = "oids[]")  Integer[]  oids){
        for(int i = 0; i < oids.length; i++){
            myOrdersService.deleteOrder(oids[i]);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/sendOrder")
    public AjaxResult sendOrder(Order order){
        myOrdersService.sendOrder(order);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
}
